using System;
using System.Collections;
using System.Collections.Generic;
using Server.Mobiles;
using Server.Items;
using Server.Gumps;
using Server.Network;


namespace Server.Gumps
{
	public class VendorBidGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int 	EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight + 2;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int HeaderTextHue = 777;

		private static readonly int NameWidth = 80;
		private static readonly int ValueWidth = 200;
		private static readonly int EntryCount = 2;

		private static readonly int TotalWidth = OffsetSize + NameWidth + OffsetSize + ValueWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (EntryCount + 1));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;

		private TradeItem m_ti;
		private int m_btOffset = 20000;

		public VendorBidGump( Mobile from, TradeItem ti ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_ti = ti;

			string name = "";
			string sval = "";

			string header = "Bid: ";
			string expdate = "";

			IHardsellable hs = ((Item)ti.ItemObj).RootParent as IHardsellable;

			if ( hs == null || hs.ShopItems == null || !( hs.ShopItems.Count > 0 ) )
				return;

			name = ti.Description;

			if ( name != null && name.Length > 23 )
				name = name.Substring( 0, 23 );

			if ( name != null && name.Length > 0 )
				header += name;

			int entryCount = EntryCount;
			if ( hs.BidItems != null && hs.BidItems.Count > 0 )
			{
				foreach ( BidByItem bbi in hs.BidItems )
				{
					if ( bbi.m_itemobj == ti.ItemObj )
					{
						expdate = bbi.Expire.ToString( "dd/MMM/yyyy HH:mm" );
						entryCount += bbi.m_byprice.Count;
					}
				}
			}

			if ( expdate == null || !( expdate.Length > 0 ) )
				expdate = "";

			CurrencyInfo cinfo = hs.CInfo;

			int itemID = cinfo.m_itemID;
			int hue = cinfo.m_hue;
			bool dupeicon = cinfo.m_dupeicon;

			int totalHeight = OffsetSize + ((EntryHeight + OffsetSize) * (entryCount + 1));

			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BorderSize + totalHeight + BorderSize, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, totalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			AddLabelCropped( x + TextOffsetX, y, ValueWidth - TextOffsetX, EntryHeight, HeaderTextHue, header );
			AddItem( TotalWidth - 60, 10, itemID, hue );

			// Credit: Lord GreyWolf of RunUO, to add 5 to both x and y to display stackables
			if ( dupeicon )
				AddItem( TotalWidth - 55, 15, itemID, hue );

			for ( int i = 0; i < entryCount; i++ )
			{
				x = BorderSize + OffsetSize;
				y += EntryHeight + OffsetSize;

				switch ( i )
				{
					case 0:
					{
						name = "Place Bid";

						if ( hs.BidItems != null && hs.BidItems.Count > 0 )
						{
							foreach ( BidByItem bbi in hs.BidItems )
							{
								if ( bbi.m_itemobj == ti.ItemObj )
								{
									foreach ( BidItem bbp in bbi.m_byprice )
									{
										if ( bbp.m_bider == from )
											sval = bbp.m_bid.ToString();
									}
								}
							}
						}

						break;
					}
					case 1:
					{
						name = "Cancel Bid"; sval = expdate;

						break;
					}
					default:
					{
						if ( hs.BidItems != null && hs.BidItems.Count > 0 )
						{
							foreach ( BidByItem bbi in hs.BidItems )
							{
								if ( bbi.m_itemobj == ti.ItemObj )
								{
									try
									{
										name = "Bid " + ( i - 1 ).ToString();
										sval = bbi.m_byprice[i - 2].m_bider.Name + " : " + bbi.m_byprice[i - 2].m_bid.ToString();
									}
									catch
									{
										Console.WriteLine( "i = {0}", i );
										from.SendMessage( "Invalid action." );
										return;
									}
								}
							}
						}

						break;
					}
				}

				AddImageTiled( x, y, NameWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, NameWidth - TextOffsetX, EntryHeight, TextHue, name );
				x += NameWidth + OffsetSize;
				AddImageTiled( x, y, ValueWidth, EntryHeight, EntryGumpID );
				AddLabelCropped( x + TextOffsetX, y, ValueWidth - TextOffsetX, EntryHeight, TextHue, sval );
				x += ValueWidth + OffsetSize;

				if ( SetGumpID != 0 )
					AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

				if ( from.AccessLevel >= AccessLevel.GameMaster || i <= 1 || ( i > 1 && hs.Owner == from ) )
					AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, i + m_btOffset + 1, GumpButtonType.Reply, 0 );
			}
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			Mobile from = state.Mobile;

			Item item = m_ti.ItemObj as Item;
			IHardsellable hs = item.RootParent as IHardsellable;

			if ( hs == null || hs.ShopItems == null || !( hs.ShopItems.Count > 0 ) )
				return;

			int adjusted = info.ButtonID - m_btOffset;

			switch ( adjusted )
			{
				case 0: // Closed
				{
					break;
				}
				case 1:	// Place bid
				{
					from.CloseGump( typeof( CommPlaceBidGump ) );
					from.SendGump( new CommPlaceBidGump( m_ti ) );

					break;
				}
				// TODO: to allow owner or staff to cancel the whole bid
				case 2:	// Cancel bid
				{
					for ( int i = 0; i < hs.BidItems.Count; i++ )
					{
						BidByItem bbi = hs.BidItems[i];

						for ( int j = 0; j < bbi.m_byprice.Count; j++ )
						{
							BidItem bi = bbi.m_byprice[j];

							if ( bi.m_bider == from )
								VendorManager.ToCancelBid( from, m_ti, bi );
						}
					}

					break;
				}
				default:
				{
					if ( !( ( adjusted - 3 ) >= 0 ) )
						return;

					if ( from == hs.Owner	|| from.AccessLevel >= AccessLevel.GameMaster )
					{
						for ( int i = 0; i < hs.BidItems.Count; i++ )
						{
							BidByItem bbi = hs.BidItems[i];
	
							if ( (Item)bbi.m_itemobj == item && bbi.m_byprice.Count > ( adjusted - 3 ) )
							{
								BidItem bi = bbi.m_byprice[adjusted - 3];
								VendorManager.ToApproveBid( from, m_ti, bi );
							}
						}
					}

					break;
				}
			}
		}

	}


	public class CommPlaceBidGump : Gump
	{
		public static readonly int GumpOffsetX = PropsConfig.GumpOffsetX;
		public static readonly int GumpOffsetY = PropsConfig.GumpOffsetY;

		public static readonly int TextHue = PropsConfig.TextHue;
		public static readonly int TextOffsetX = PropsConfig.TextOffsetX;

		public static readonly int OffsetGumpID = PropsConfig.OffsetGumpID;
		public static readonly int HeaderGumpID = PropsConfig.HeaderGumpID;
		public static readonly int  EntryGumpID = PropsConfig.EntryGumpID;
		public static readonly int   BackGumpID = PropsConfig.BackGumpID;
		public static readonly int    SetGumpID = PropsConfig.SetGumpID;

		public static readonly int SetWidth = PropsConfig.SetWidth;
		public static readonly int SetOffsetX = PropsConfig.SetOffsetX, SetOffsetY = PropsConfig.SetOffsetY;
		public static readonly int SetButtonID1 = PropsConfig.SetButtonID1;
		public static readonly int SetButtonID2 = PropsConfig.SetButtonID2;

		public static readonly int PrevWidth = PropsConfig.PrevWidth;
		public static readonly int NextWidth = PropsConfig.NextWidth;

		public static readonly int OffsetSize = PropsConfig.OffsetSize;

		public static readonly int EntryHeight = PropsConfig.EntryHeight;
		public static readonly int BorderSize = PropsConfig.BorderSize;

		private static readonly int EntryWidth = 212;

		private static readonly int TotalWidth = OffsetSize + EntryWidth + OffsetSize + SetWidth + OffsetSize;
		private static readonly int TotalHeight = OffsetSize + (2 * (EntryHeight + OffsetSize));

		private static readonly int BackWidth = BorderSize + TotalWidth + BorderSize;
		private static readonly int BackHeight = BorderSize + TotalHeight + BorderSize;

		private static string[] m_Names = new string[]{ "True", "False" };

		private TradeItem m_ti;
		private int m_btOffset = 12000;

		public CommPlaceBidGump( TradeItem ti ) : base( GumpOffsetX, GumpOffsetY )
		{
			m_ti = ti;

			IHardsellable hs = ((Item)ti.ItemObj).RootParent as IHardsellable;

			if ( hs == null || ti == null )
				return;

			string title = "";
			string initialText = "";


				title = "BID: ";

				if ( ti.Description != null && ti.Description.Length > 0 )
					title += ti.Description;
				else
					title += VendorManager.ToGetName( (Item)ti.ItemObj, false );

				if ( ti.Price > 0 )
					initialText = ti.Price.ToString();


			AddPage( 0 );

			AddBackground( 0, 0, BackWidth, BackHeight, BackGumpID );
			AddImageTiled( BorderSize, BorderSize, TotalWidth, TotalHeight, OffsetGumpID );

			int x = BorderSize + OffsetSize;
			int y = BorderSize + OffsetSize;

			AddImageTiled( x, y, EntryWidth, EntryHeight, EntryGumpID );
			AddLabelCropped( x + TextOffsetX, y, EntryWidth - TextOffsetX, EntryHeight, TextHue, title );
			x += EntryWidth + OffsetSize;

			if ( SetGumpID != 0 )
				AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

			x = BorderSize + OffsetSize;
			y += EntryHeight + OffsetSize;

			AddImageTiled( x, y, EntryWidth, EntryHeight, EntryGumpID );
			AddTextEntry( x + TextOffsetX, y, EntryWidth - TextOffsetX, EntryHeight, TextHue, 0, initialText );
			x += EntryWidth + OffsetSize;

			if ( SetGumpID != 0 )
				AddImageTiled( x, y, SetWidth, EntryHeight, SetGumpID );

			AddButton( x + SetOffsetX, y + SetOffsetY, SetButtonID1, SetButtonID2, m_btOffset + 1, GumpButtonType.Reply, 0 );
		}

		public override void OnResponse( NetState state, RelayInfo info )
		{
			Mobile from = state.Mobile;
			int adjusted = 0;

			if ( info.ButtonID > 0 )
				adjusted = info.ButtonID - m_btOffset;

			IHardsellable hs = ((Item)m_ti.ItemObj).RootParent as IHardsellable;

			if ( hs == null || m_ti == null )
			{
				from.SendMessage( "Error: vendor or item null." );
				return;
			}

			switch ( adjusted )
			{
				case 0: // Closed
				{

					break;
				}
				case 1: // Bid
				{
					TextRelay text = info.GetTextEntry( 0 );

					int bid = 0;
					int value;
					bool parsed = Int32.TryParse( text.Text, out value );

					if ( parsed )
					{
						value = ( value > 100000000 ? 100000000 : value );

						bid = Math.Abs( value );
					}

					VendorManager.ToPlaceBid( from, m_ti, bid );
					break;
				}
			}
		}
	}
}